﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections;
using UrlRewritingNet.Web;
using System.Collections.Specialized;


/// <summary>
/// Summary description for ULRHelper
/// </summary>
public class URLHelper
{
    static string AppPath = HttpContext.Current.Request.ApplicationPath + (HttpContext.Current.Request.ApplicationPath.EndsWith("/") ? "" : "/");
    public URLHelper()
    {
      
    }
    public static string FilterURLCharacters(string url)
    {
        url = UnicodeStrings.LatinToAscii(url);
        return Regex.Replace(url.Trim(), "[^a-zA-Z0-9/]+", "-");
    }
    public static string BuildURL(string page)
    {
        return string.Format(AppPath + "{0}.html", page);
    }
    public static string BuildURLCATE(string page,string cate)
    {
        return string.Format(AppPath + "{0}/{1}.html", page,cate);
    }
    //public static string BuildPagelURL(string page, int articleID, string articleName)
    //{

    //    return string.Format(AppPath + "{0}/{1}-a{2}.aspx", page, FilterURLCharacters(articleName), articleID);
    //}
    /// <summary>
    /// build url of an article/category base on their article type
    /// </summary>
    /// <param name="page">the string representative if article type. It must be define in url rewriting config</param>
    /// <param name="articleID">article id</param>
    /// <param name="articleName">article name</param>
    /// <returns>url</returns>
    //public static string BuildAboutUsDetailURL(string page, int articleID, string articleName)
    //{

    //    return string.Format(AppPath + "{0}/{1}-a{2}.aspx", page, FilterURLCharacters(articleName), articleID);
    //}


    //public static string BuildCensorshipDetailURL(string page, int articleID, string articleName)
    //{

    //    return string.Format(AppContext.AppPath + "{0}/{1}-a{2}.aspx", page, FilterURLCharacters(articleName), articleID);
    //}
    //public static string BuildDeleteArticleURL(int articleID)
    //{

    //    return string.Format(AppContext.AppPath + "quan-ly-tai-khoan/xoa-a{0}.aspx", articleID);
    //}
    //public static string BuildSubURL(string page, int articleID, string articleName)
    //{

    //    return string.Format(AppContext.AppPath + "{1}-n{0}/{2}.aspx", articleID, page, FilterURLCharacters(articleName));
    //}
    //public static string BuildSubURL(string page, int articleID, int categoryID, string articleName)
    //{
    //    return string.Format(AppContext.AppPath + "{2}-n{0}c{1}/{3}.aspx", articleID, categoryID, page, FilterURLCharacters(articleName));
    //}

    //public static string BuildSubCategoryURL(string page, int categoryID, string categoryName)
    //{
    //    return string.Format(AppContext.AppPath + "{1}-c{0}/{2}.aspx", categoryID, page, FilterURLCharacters(categoryName));
    //}

   
    //public static string BuildURL(string page)
    //{
    //    return string.Format(AppContext.AppPath + "{0}.aspx", page);
    //}
    //public static string BuildURL(string page, params string[] args)
    //{
    //    string queryString = "";
    //    if (args.Length > 0)
    //    {
    //        string query = "";
    //        for (int i = 0; i < args.Length; i++)
    //        {
    //            if (i % 2 == 0)
    //                query = args[i];
    //            else
    //            {
    //                query += "=" + args[i];
    //                queryString += (queryString == "" ? "" : "&") + query;
    //            }
    //        }
    //        if (queryString != "") queryString = "?" + queryString;
    //    }
    //    return string.Format(AppContext.AppPath + "{0}.aspx{1}", page, queryString);
    //}
    ///// <summary>
    ///// Build url of an article detail
    ///// </summary>
    ///// <returns></returns>
    //public static string BuildArticleURL(int articleID, string articleName, int typeid)
    //{
    //    return string.Format(AppContext.AppPath + "ad-{1}-{2}/{0}.aspx", FilterURLCharacters(articleName), articleID, typeid);
    //}

    ///// <summary>
    ///// Build url of an custom article list
    ///// </summary>
    ///// <returns></returns>
    //public static string BuildArticleListURL(int typeid, string typename)
    //{

    //    return string.Format(AppContext.AppPath + "at-{1}/{0}.aspx", FilterURLCharacters(typename), typeid);
    //}


    ///// <summary>
    ///// news
    ///// </summary>
    ///// <param name="articleID"></param>
    ///// <param name="articleName"></param>
    ///// <returns></returns>
    //public static string BuildNewsURL(int articleID, string articleName)
    //{
    //    return string.Format(AppContext.AppPath + "TinTuc-n{0}/{1}.aspx", articleID, FilterURLCharacters(articleName));
    //}

    //public static string BuildNewsURL(int articleID, int categoryID, string articleName)
    //{
    //    if (categoryID != 0)
    //        return string.Format(AppContext.AppPath + "TinTuc-n{0}c{1}/{2}.aspx", articleID, categoryID, FilterURLCharacters(articleName));
    //    else
    //        return string.Format(AppContext.AppPath + "TinTuc-n{0}/{1}.aspx", articleID, FilterURLCharacters(articleName));
    //}

    //public static string BuildNewsCategoryURL(int categoryID, string categoryName)
    //{
    //    return string.Format(AppContext.AppPath + "TinTuc-c{0}/{1}.aspx", categoryID, FilterURLCharacters(categoryName));
    //}
    ///// <summary>
    ///// service url
    ///// </summary>
    ///// <param name="articleID"></param>
    ///// <param name="articleName"></param>
    ///// <returns></returns>
    //public static string BuildServiceURL(int articleID, string articleName)
    //{
    //    return BuildServiceURL(articleID, articleName, 0);
    //}

    //public static string BuildProductCategoryBottom(int type, int categoryID, string categoryPath)
    //{
    //    if (type == 18)
    //    {
    //        return BuildProductCategory("Thoi-trang", categoryID, categoryPath);
    //    }
    //    else if (type == 19)
    //    {
    //        return BuildProductCategory("Bat-dong-san", categoryID, categoryPath);
    //    }
    //    else
    //    {
    //        return BuildProductCategory("Dien-may", categoryID, categoryPath);
    //    }

    //}

    //public static string BuildProductCategory(string page, int categoryID, string categoryPath)
    //{
    //    return string.Format(AppContext.AppPath + "{0}/{1}-c{2}.aspx", page, FilterURLCharacters(categoryPath), categoryID);
    //}
    //public static string BuildProductDetailByType(int type, int articleID, string articlePath)
    //{
    //    if (type == 18)
    //    {
    //        return string.Format(AppContext.AppPath + "{0}/{1}-a{2}.aspx", "Thoi-trang/thong-tin-chi-tiet", FilterURLCharacters(articlePath), articleID);

    //    }
    //    else if (type == 19)
    //    {
    //        return string.Format(AppContext.AppPath + "{0}/{1}-a{2}.aspx", "Bat-dong-san/thong-tin-chi-tiet", FilterURLCharacters(articlePath), articleID);

    //    }
    //    else
    //    {
    //        return string.Format(AppContext.AppPath + "{0}/{1}-a{2}.aspx", "Dien-may/thong-tin-chi-tiet", FilterURLCharacters(articlePath), articleID);

    //    }

    //}
    //public static string BuildProductDetail(string page, int articleID, string articlePath)
    //{
    //    return string.Format(AppContext.AppPath + "{0}/{1}-a{2}.aspx", page, FilterURLCharacters(articlePath), articleID);
    //}
    //public static string BuildServiceURL(int articleID, string articleName, int categoryID)
    //{
    //    if (categoryID != 0)
    //        return string.Format(AppContext.AppPath + "s{0}c{1}/{2}.aspx", articleID, categoryID, FilterURLCharacters(articleName));
    //    else
    //        return string.Format(AppContext.AppPath + "s{0}/{1}.aspx", articleID, FilterURLCharacters(articleName));
    //}

    //public static string BuildServiceCategoryURL(int categoryID, string categoryName)
    //{
    //    return string.Format(AppContext.AppPath + "cs{0}/{1}.aspx", categoryID, FilterURLCharacters(categoryName));
    //}

    ///// <summary>
    ///// product
    ///// </summary>
    ///// <param name="articleID"></param>
    ///// <param name="articleName"></param>
    ///// <returns></returns>
    //public static string BuildProductURL(int articleID, string articleName)
    //{
    //    return BuildProductURL(articleID, articleName, 0);
    //}

    //public static string BuildProductURL(int articleID, string articleName, int categoryID)
    //{
    //    if (categoryID == 0)
    //        return string.Format(AppContext.AppPath + "SanPham-n{0}/{1}.aspx", articleID, FilterURLCharacters(articleName));
    //    else
    //        return string.Format(AppContext.AppPath + "SanPham-n{0}c{1}/{2}.aspx", articleID, categoryID, FilterURLCharacters(articleName));
    //}

    //public static string BuildCategoryURL(int categoryID, string categoryName)
    //{
    //    return string.Format(AppContext.AppPath + "SanPham-c{0}/{1}.aspx", categoryID, FilterURLCharacters(categoryName));
    //}

    //public static string BuildCategoryURL(string page, int categoryID, string categoryName)
    //{
    //    return string.Format(AppContext.AppPath + "SanPham-c{0}/{1}/{2}.aspx", categoryID, page, FilterURLCharacters(categoryName));
    //}

    ///// <summary>
    ///// download
    ///// </summary>
    ///// <param name="fileID"></param>
    ///// <returns></returns>
    //public static string BuildDownloadURL(int fileID)
    //{
    //    return string.Format(AppContext.AppPath + "download{0}/{0}.aspx", fileID);
    //}

    //public static string BuildForumURL(int articleID, string articleName)
    //{
    //    return string.Format(AppContext.AppPath + "f{0}/{1}.aspx", articleID, FilterURLCharacters(articleName));
    //}

    //public static bool CheckPage(params string[] args)
    //{
    //    NameValueCollection queries = HttpContext.Current.Request.QueryString;
    //    if (args.Length > 0)
    //    {
    //        bool ret = true;
    //        for (int i = 0; i < args.Length && ret; i += 2)
    //        {
    //            string pName = args[i];
    //            if (queries[pName] == null)
    //                ret = false;
    //            else
    //            {
    //                if (i + 1 < args.Length)
    //                {
    //                    string[] val = args[i + 1].ToLower().Split(new char[] { '|', ';' });
    //                    ret = Array.IndexOf(val, queries[pName].ToLower()) >= 0;
    //                }
    //            }
    //        }
    //        return ret;

    //    }
    //    else return false;
    //}

    //public static string FilterURLCharacters(string url)
    //{
    //    url = Unichar.UnicodeStrings.LatinToAscii(url);
    //    return Regex.Replace(url.Trim(), "[^a-zA-Z0-9/]+", "-");
    //}

    //public static string BuildBreadcrumb(string path, string rootText, string rootPage, string branch)
    //{
    //    return BuildBreadcrumb(path, rootText, rootPage, branch, true);
    //}

    //public static string BuildBreadcrumb(string path, string rootText, string rootPage, string branch, bool removeLastNode)
    //{
    //    ArrayList lst = new ArrayList();
    //    lst.Add(BuildURL(rootPage));
    //    lst.Add(rootText);
    //    string[] paths = path.Split(new char[] { '|' });
    //    for (int i = 0; i < paths.Length; i++)
    //    {
    //        int catID = 0;
    //        string catName = string.Empty;
    //        if (paths[i].IndexOf("#") > 0)
    //        {
    //            catID = int.Parse(paths[i].Substring(0, paths[i].IndexOf("#")));
    //            catName = paths[i].Substring(paths[i].IndexOf("#") + 1);
    //        }
    //        else
    //        {
    //            catName = paths[i].Substring(paths[i].IndexOf("#") + 1);
    //        }
    //        //url += " » " + string.Format("<a href='{0}{1}{2}/{3}.aspx'>{4}</a>", AppContext.AppFullPath, branch, catID, FilterURLCharacters(catName), catName);
    //        //url += " » " + string.Format("<a href='{0}{1}{2}/{3}/{4}.aspx'>{5}</a>", AppContext.AppFullPath, branch, catID, rootPage, FilterURLCharacters(catName), catName);
    //        if ((i < paths.Length - 1 || !removeLastNode) && catID > 0)
    //            lst.Add(string.Format("{0}{4}-{1}{2}/{3}.aspx", AppContext.AppFullPath, branch, catID, FilterURLCharacters(catName), rootPage));
    //        lst.Add(catName);
    //    }
    //    return Breadcrumb(lst.ToArray(typeof(string)) as string[]);
    //}

    //public static string Breadcrumb(string[] breadcrumb)
    //{
    //    StringBuilder sb = new StringBuilder();
    //    CategoryBLL bllCate = new CategoryBLL(AppContext.CurrentSiteID);
    //    ListBase<Category> listCate = null;

    //    sb.AppendFormat("<div style='float:left'><a href='{0}'>{1}</a>&nbsp;»&nbsp;</div>", BuildURL("Home"), global::Resources.Resource.home);
    //    for (int i = 0; i < breadcrumb.Length; i = i + 2)
    //    {
    //        if (i == breadcrumb.Length - 1)
    //            sb.Append(breadcrumb[i]);
    //        else
    //        {
    //            if (i == 0)
    //                sb.AppendFormat("<div style='float:left'><a href='{0}'>{1}</a></div>", breadcrumb[i], breadcrumb[i + 1]);
    //            //New
    //            else
    //            {
    //                sb.AppendFormat("<div id='dropdown'  onclick='show_dropdown_" + i + "()' ><a href='" + breadcrumb[i] + "'>" + HTMLHelper.ReadBlock(breadcrumb[i + 1], 5) + "</a><div id='dd_list_" + i + "' onmousemove='show_dropdown_" + i + "()' onmouseout='hide_list_" + i + "()' ><div id='ul'>");

    //                string bred = breadcrumb[i];
    //                int cateID = 0;
    //                if (bred != "")
    //                {
    //                    string[] spl = bred.Split(new char[] { '-' });
    //                    if (spl[1].IndexOf("/") > 0)
    //                    {
    //                        cateID = int.Parse(spl[1].Substring(1, (spl[1].IndexOf("/") - 1)));
    //                    }
    //                }
    //                Category cate = bllCate.GetByID(cateID);
    //                if (cate != null)
    //                {
    //                    int parentID = cate.ParentCateID;
    //                    if (parentID > 0)
    //                        listCate = bllCate.GetDynamic(string.Format("ParentCateID={0} and CategoryID<>{1}", parentID, cateID), "SortOrder asc", AppContext.CurrentLanguage.LanguageID);
    //                    else listCate = bllCate.GetDynamic(string.Format("Type={0} and ParentCateID={1} and CategoryID<>{2}", (int)EnumArticleTypes.Products, 0, cateID), "SortOrder asc", AppContext.CurrentLanguage.LanguageID);
    //                }
    //                if (listCate != null)
    //                {
    //                    for (int k = 0; k < listCate.Count; k++)
    //                    {
    //                        sb.AppendFormat("<div class='margin-br'><a href='" + bred.Replace("c" + cateID.ToString(), "c" + listCate[k].CategoryID.ToString()) + "'>" + HTMLHelper.ReadBlock(listCate[k].CategoryName, 5) + "</a></div>");
    //                    }
    //                }
    //                sb.AppendFormat("</div></div></div>");
    //            }
    //            //end New  
    //        }

    //        if (i < breadcrumb.Length - 2)
    //            sb.Append("<div style='float:left'>&nbsp;»&nbsp;</div>");

    //    }
    //    return sb.ToString();
    //}

    //public static string BuildBreadcrumb(string path, string type)
    //{
    //    return "";
    //}
    //public static string BuildBreadcrumb(string path)
    //{

    //    return BuildBreadcrumb(path, global::Resources.Resource.product, "Products", "c");
    //}
    //public static string BuildNewsBreadcrumb(string path)
    //{
    //    return BuildBreadcrumb(path, global::Resources.Resource.news, "News", "cn");

    //}

    //public static string BuildServicesBreadcrumb(string path)
    //{
    //    string url = "<a href='{0}'>{1}</a>";
    //    url = string.Format(url, BuildURL("Services"), global::Resources.Resource.services);
    //    string[] paths = path.Split(new char[] { '|' });
    //    for (int i = 0; i < paths.Length - 1; i++)
    //    {
    //        int catID = int.Parse(paths[i].Substring(0, paths[i].IndexOf("#")));
    //        string catName = paths[i].Substring(paths[i].IndexOf("#") + 1);
    //        url += " > " + string.Format("<a href='{0}'>{1}</a>", BuildServiceCategoryURL(catID, catName), catName);
    //    }

    //    return url;
    //}

    //public static bool CheckAdminPage(params string[] pageNames)
    //{
    //    string page = "0";
    //    if (HttpContext.Current.Request["Active"] != null)
    //    {
    //        HttpContext.Current.Session["ReqActive"] = HttpContext.Current.Request["Active"];
    //        page = HttpContext.Current.Session["ReqActive"].ToString();
    //    }

    //    if (HttpContext.Current.Session["ReqActive"] != null)
    //        page = HttpContext.Current.Session["ReqActive"].ToString();

    //    for (int i = 0; i < pageNames.Length; i++)
    //    {
    //        if (page.ToLower() == pageNames[i].ToLower())
    //            return true;
    //    }
    //    return false;
    //}


    //public static string BuildKeywordLink(string keyword)
    //{ return BuildKeywordLink(keyword, "Products"); }
    //public static string BuildKeywordLink(string keyword, string type)
    //{
    //    string url = string.Empty;

    //    string[] keywords = keyword.Split(new char[] { ',' });
    //    foreach (string s in keywords)
    //    {
    //        url += string.Format("<a href='{0}'>{1}</a>&nbsp;&nbsp;&nbsp;", BuildURL("Search", "keyword", s.Trim(), "type", type), s.Trim());
    //    }

    //    return url;
    //}

    //public static string AppendQueryString(string url, string name, string val)
    //{
    //    //VNS.Utils.Write2Log.WriteLogs("URLHelper", "AppendQueryString Begin", url);
    //    Regex reg = new Regex(string.Format("{0}=[^&#]*", name));
    //    if (reg.IsMatch(url))
    //        url = reg.Replace(url, string.Format(@"{0}={1}", name, val));
    //    else
    //    {
    //        string bookmark = string.Empty;
    //        if (Regex.IsMatch(url, "#.+"))
    //        {
    //            bookmark = Regex.Match(url, "#.+").Value;
    //            url = url.Replace(bookmark, "");
    //        }
    //        if (Regex.IsMatch(url, @"\?.+"))
    //            url += "&" + name + "=" + val + bookmark;
    //        else
    //            url = url.Replace("?", "") + "?" + name + "=" + val + bookmark;
    //    }
    //    VNS.Utils.Write2Log.WriteLogs("URLHelper", "AppendQueryString End", url);
    //    return url;
    //}

    //public static string BuildTooltipURL(int articleID, string articleName)
    //{
    //    return BuildURL("Tooltip", "ArticleID", articleID.ToString());
    //}

    //public static string GetSubPage(EnumArticleTypes articleType)
    //{
    //    string subpage = "TinTuc";
    //    switch (articleType)
    //    {
    //        case EnumArticleTypes.AboutUs:
    //            subpage = "GioiThieu";
    //            break;
    //        case EnumArticleTypes.Forum:
    //            subpage = "HoiDap";
    //            break;
    //        case EnumArticleTypes.News:
    //            subpage = "TinTuc";
    //            break;
    //        case EnumArticleTypes.Process:
    //            subpage = "NhaMay";
    //            break;
    //        case EnumArticleTypes.Products:
    //            subpage = "SanPham";
    //            break;
    //        case EnumArticleTypes.Services:
    //            subpage = "DichVu";
    //            break;
    //        case EnumArticleTypes.Recruitment:
    //            subpage = "TuyenDung";
    //            break;
    //        default:
    //            subpage = "TinTuc";
    //            break;
    //    }
    //    return subpage;
    //}

    //public static string GetSubText(EnumArticleTypes articleType)
    //{
    //    string subpage = "TinTuc";
    //    switch (articleType)
    //    {
    //        case EnumArticleTypes.AboutUs:
    //            subpage = global::Resources.Resource.aboutus;
    //            break;
    //        case EnumArticleTypes.Forum:
    //            subpage = global::Resources.Resource.forum;
    //            break;
    //        case EnumArticleTypes.News:
    //            subpage = global::Resources.Resource.news;
    //            break;
    //        case EnumArticleTypes.Products:
    //            subpage = global::Resources.Resource.product;
    //            break;
    //        case EnumArticleTypes.Services:
    //            subpage = global::Resources.Resource.service;
    //            break;
    //        case EnumArticleTypes.Process:
    //            subpage = global::Resources.Resource.factory;
    //            break;
    //        case EnumArticleTypes.Recruitment:
    //            subpage = global::Resources.Resource.recruitment;
    //            break;
    //        default:
    //            subpage = global::Resources.Resource.news;
    //            break;
    //    }
    //    return subpage;
    //}
}
